using System.Threading;

namespace System.Utility
{
	public sealed class SingleThread
	{
		public Thread BackThread
		{
			get;
			private set;
		}

		public bool IsAlive
		{
			get
			{
				return (BackThread == null ? false : BackThread.IsAlive);
			}
		}

		public SingleThread()
		{
		}

		public void Continue()
		{
			if (BackThread.ThreadState == ThreadState.Suspended)
			{
				BackThread.Resume();
			}
		}

		public void Parse()
		{
			BackThread.Suspend();
		}

		public void Start(Action fun)
		{
			BackThread = new Thread(new ThreadStart(fun.Invoke))
			{
				IsBackground = true
			};
			BackThread.Start();
		}

		public void Stop(Action callback = null)
		{
			if ((BackThread == null ? false : BackThread.IsAlive))
			{
				try
				{
					try
					{
						if (callback != null)
						{
							callback();
						}
						if (BackThread.ThreadState == ThreadState.Suspended)
						{
							BackThread.Resume();
						}
						BackThread.Abort();
					}
					catch (ThreadAbortException threadAbortException)
					{
					}
				}
				finally
				{
					BackThread.DisableComObjectEagerCleanup();
				}
			}
		}
	}
}